<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use app\common\model\Order;
use app\common\model\User;
/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $time_start = strtotime(date('Y-m-d',time()));
        $time_end = strtotime(date('Y-m-d'),time())+86400;
        $where['status']  = array('in','1,2,3,6');
        //$where['o.createtime']  = array('between',"$time_start,$time_end");
        //平台总收入
        $total_money = Order::where($where)->sum('actual_price');
        //平台会员总人数
        $user_total_count = User::count();
        //总订单量
        $total_order = Order::count();
        //已完成订单量
        $success_order_count = Order::where('status',3)->count();
        //退款订单数量
        $refund_order_count = Order::where('status',5)->count();
        //一级会员人数
        $first_total_count = User::where('group_id','2')->count();
        //二级会员人数
        $second_total_count = User::where('group_id','3')->count();
        //三级会员人数
        $third_total_count = User::where('group_id','4')->count();
        $this->view->assign("total_money", $total_money);
        $this->view->assign("success_order_count", $success_order_count);
        $this->view->assign("refund_order_count", $refund_order_count);
        $this->view->assign("total_order", $total_order);
        $this->view->assign("user_total_count", $user_total_count);
        $this->view->assign("first_total_count", $first_total_count);
        $this->view->assign("second_total_count", $second_total_count);
        $this->view->assign("third_total_count", $third_total_count);

        return $this->view->fetch();
    }
    public function search(){
        if ($this->request->isAjax())
        {
            $time = $_POST['time'];
            $time = explode('-',$time);
            $start_time = strtotime($time[0]);
            $end_time = strtotime($time[1]);
            $where['status']  = array('in','1,2,3,6');
            $where['createtime']  = array('between',"$start_time,$end_time");
            $total_money = Order::where($where)->sum('actual_price');
            $total_money = sprintf("%.2f",substr(sprintf("%.3f", $total_money), 0, -2));
            //订单总数量
            //总订单量
            $total_order = Order::where('createtime','between',"$start_time,$end_time")->count();
            //已完成订单量
            $success_order_count = Order::where('createtime','between',"$start_time,$end_time")->where('status',3)->count();
            //退款订单数量
            $refund_order_count = Order::where('createtime','between',"$start_time,$end_time")->where('status',5)->count();
            $result = array("total_money"=>$total_money,"total_order"=>$total_order,"success_order_count"=>$success_order_count,"refund_order_count"=>$refund_order_count,);
            return $this->success('', null, $result);
        }
    }

}
